package com.huabo.contract.oracle.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import com.huabo.contract.oracle.entity.TblOrganization;

import com.huabo.contract.util.Tree;
import org.apache.ibatis.annotations.*;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;


/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author huabo
 * @since 2021-10-19
 */
public interface TblOrganizationMapper extends BaseMapper<TblOrganization> {

	/**
	 * 传入部门Id 查找出当前部门所隶属的公司
	 * @param
	 * @return
	 * @throws Exception
	 */
	@Select("select o.ORGID,o.ORGNAME,o.FATHERORGID,o.ORGNUMBER,o.ORGMENO,o.MEMO,o.ICODE,o.ORGTYPE,o.STATUS,o.ISZY from TBL_ORGANIZATION o where o.ORGTYPE != 0 AND ROWNUM = 1 START WITH o.ORGID = #{orgid} CONNECT by PRIOR o.FATHERORGID = o.ORGID order BY o.ORGTYPE desc")
	@Results({
		@Result(column="ORGID",property="orgid"),
		@Result(column="ORGNAME",property="orgname"),
		@Result(column="FATHERORGID",property="fatherorgid"),
		@Result(column="ORGNUMBER",property="orgnumber"),
		@Result(column="ORGMENO",property="orgmeno"),
		@Result(column="MEMO",property="memo"),
		@Result(column="ICODE",property="icode"),
		@Result(column="ORGTYPE",property="orgtype"),
		@Result(column="STATUS",property="status"),
		@Result(column="ISZY",property="iszy"),
	})
	TblOrganization selectCompanyInfoByDeptId(@Param("orgid") BigDecimal orgid) throws Exception;

	@Select("SELECT COUNT(0) FROM TBL_ORGANIZATION WHERE ORGNAME LIKE '%审计%' AND ORGID = (SELECT ORGID FROM TBL_STAFF WHERE STAFFID = #{staffid})")
    Integer findCountByUserSjb(BigDecimal staffid);

    @Select("SELECT * FROM TBL_ORGANIZATION WHERE ORGID= #{linkdept}")
	TblOrganization findByLinkDept(String linkdept);

	@Select("SELECT * FROM TBL_ORGANIZATION WHERE ORGID = #{nodeId}")
	List<Tree> getNodes(BigDecimal nodeId);

	@Select("select * from TBL_ORGANIZATION where ORGID = #{nodeId} and STATUS=0 and ORGTYPE < 100 ORDER BY orderid ASC")
	List<TblOrganization> findBysql(BigDecimal nodeId);

	@Select("SELECT * FROM TBL_ORGANIZATION WHERE ORGID = #{pid}")
    BigDecimal selectPid(String pid);
	@Select("select * from TBL_ORGANIZATION  WHERE ORGTYPE<100 and ORGTYPE != 0 and FATHERORGID= #{orgid} and  STATUS = 0 ORDER BY ORDERID ASC")
    List<TblOrganization> findByOrgid(BigDecimal orgid);

	@Select("SELECT * FROM TBL_ORGANIZATION WHERE ORGID = #{orgid}")
	TblOrganization findById(BigDecimal orgid);

	@Select("SELECT * FROM TBL_ORGANIZATION  t where t.FATHERORGID = #{nodeId} and STATUS = 0 order by t.ORDERID asc")
	List<TblOrganization> getNode(BigDecimal nodeId);

	@Select("SELECT * FROM TBL_ORGANIZATION WHERE FATHERORGID = #{orgid}")
    Set<TblOrganization> findByfatherorgId(BigDecimal orgid);

	@InsertProvider(method="saveToz",type=TblOrganizationMapperSqlConfig.class)
    void saveToz(TblOrganization toz);

	@UpdateProvider(method="updateToz",type=TblOrganizationMapperSqlConfig.class)
	void updateToz(TblOrganization toz);
	@Select("SELECT WRITTENBYDEPT FROM TBL_ORGANIZATION WHERE ORGID = #{orgid}")
	String selectWrittenDeptByOrgId(BigDecimal orgid);
	
	@Select("SELECT WRITTENBYDEPT FROM TBL_ORGANIZATION WHERE ORGID = (SELECT ORGID FROM TBL_STAFF WHERE STAFFID = #{createuser})")
	String selectWrittenDeptByUserId(BigDecimal createuser);
    @Select("SELECT * from TBL_ORGANIZATION WHERE HISTORYCODE=#{historyId}")
	List<TblOrganization> findByHistoryId(String historyId);
    @Select("SELECT * from TBL_ORGANIZATION WHERE ORGNUMBER=#{orgnumber}")
    List<TblOrganization> findByOrgNumber(String orgnumber);
    @Select("SELECT * from TBL_ORGANIZATION WHERE DATASOURCE like'%YJ%' AND FATHERORGID IS NULL AND HISTORYCODE != '0001A71000000000EL00'")
    List<TblOrganization> findListWheBt();
	
	//==
	@Select("SELECT * from TBL_ORGANIZATION  t where t.fatherorgid = #{nodeId} order by t.orderid asc")
	List<TblOrganization> selectChildrenOrgList(BigDecimal nodeId) throws Exception;
}
